﻿using BCCommon;
using BCEntity.Common.EvaluationTag;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Common.EvaluationTag
{
    public class EvaluationTagData : IEvaluationTagData
    {
        private const string QUERY = @"SELECT EvaluationTagId,UserId,DriverId,TagContent,UseCount,CreateTime,UpdateTime FROM EvaluationTag";
        private readonly IDatabaseContext databaseContext;

        public EvaluationTagData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<long> CheckExists(long userId, string tagContent)
        {
            string sql = @"SELECT EvaluationTagId FROM EvaluationTag WHERE UserId=?UserId AND TagContent=?TagContent LIMIT 1";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.ExecuteScalarAsync<long>(sql, parameters);
        }

        public async Task<long> CheckExists(string tagContent, long driverId)
        {
            string sql = @"SELECT EvaluationTagId FROM EvaluationTag WHERE DriverId=?DriverId AND TagContent=?TagContent LIMIT 1";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.ExecuteScalarAsync<long>(sql, parameters);
        }

        public async Task<long> CheckExists(long evaluationTagId)
        {
            string sql = @"SELECT EvaluationTagId FROM EvaluationTag WHERE EvaluationTagId=?EvaluationTagId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?EvaluationTagId", evaluationTagId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.ExecuteScalarAsync<long>(sql, parameters);
        }

        public async Task<EvaluationTagEntity> Create(EvaluationTagEntity evaluationTagEntity)
        {
            string sql = SqlCommands.GetInsertSql("EvaluationTag", new string[] { "UserId", "DriverId", "TagContent" });
            sql = string.Format("{0} {1} WHERE EvaluationTagId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", evaluationTagEntity.UserId, DbType.Int64, size: 20);
            parameters.Add("?DriverId", evaluationTagEntity.DriverId, DbType.Int64, size: 20);
            parameters.Add("?TagContent", evaluationTagEntity.TagContent, DbType.AnsiString, size: 30);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EvaluationTagEntity>(sql, parameters);
        }

        public async Task<IEnumerable<EvaluationTagEntity>> GetInComeUseEntities(long? userId, long? driverId)
        {
            string sql = string.Empty;
            DynamicParameters parameters = new DynamicParameters();

            if (userId.HasValue)
            {
                sql = "WHERE UserId=?UserId";
                parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            }
            if (driverId.HasValue)
            {
                sql = "WHERE DriverId=?DriverId";
                parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            }

            return await this.databaseContext.Slave.QueryAsync<EvaluationTagEntity>(string.Format("{0} {1} ORDER BY UseCount DESC,CreateTime DESC LIMIT 0,10", QUERY, sql), parameters);
        }

        public async Task<EvaluationTagEntity> UpdateUseCount(long evaluationTagId)
        {
            string sql = @"UPDATE EvaluationTag SET UseCount=UseCount+1 WHERE EvaluationTagId=?EvaluationTagId;";
            sql = string.Format("{0} {1} WHERE EvaluationTagId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?EvaluationTagId", evaluationTagId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EvaluationTagEntity>(sql, parameters,databaseContext.Transaction);
        }
    }
}
